package com.moli.iov.common.hbase.enums;

import java.util.Arrays;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

/**
 *
 * HBase thread pool executor policy.
 *
 * @author OAK
 * @version  3.0.1
 * @since 2019/12/13 10:12:00 PM.
 *
 */
public enum ThreadPoolExecutorPolicy {

    /**
     * Thread pool executor caller runs policy.
     */
    CALLERRUNSPOLICY("CALLERRUNSPOLICY", new ThreadPoolExecutor.CallerRunsPolicy()),
    /**
     * Thread pool executor discard policy.
     */
    DISCARDPOLICY("DISCARDPOLICY", new ThreadPoolExecutor.DiscardPolicy()),
    /**
     * Thread pool executor discard oldest policy.
     */
    DISCARDOLDESTPOLICY("DISCARDOLDESTPOLICY", new ThreadPoolExecutor.DiscardOldestPolicy()),
    /**
     * Thread pool executor abort policy.
     */
    ABORTPOLICY("ABORTPOLICY", new ThreadPoolExecutor.AbortPolicy());

    /**
     * Thread pool rejected policy for execution handler.
     */
    private RejectedExecutionHandler value;

    /**
     * Thread pool rejected policy for name.
     */
    private String displayName;

    ThreadPoolExecutorPolicy(String displayName, RejectedExecutionHandler value){
        this.displayName = displayName;
        this.value = value;
    }

    /**
     * According to display name for Look up value.
     * @param displayName Display name.
     * @return After for look up value.
     */
    public static RejectedExecutionHandler lookupForDisplayName(String displayName){
       return Arrays.stream(ThreadPoolExecutorPolicy.values())
                .filter(threadPoolExecutorPolicy -> threadPoolExecutorPolicy.getDisplayName().equalsIgnoreCase(displayName))
                .findFirst().orElse(ABORTPOLICY).getValue();
    }

    public RejectedExecutionHandler getValue() {
        return value;
    }

    public void setValue(RejectedExecutionHandler value) {
        this.value = value;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }
}
